package com.chenwc.util.multithread;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.chenwc.enumeration.LocaleLanguage;
import com.chenwc.enumeration.TranslateSoftware;
import com.chenwc.util.JarFileSignEntity;
import com.chenwc.util.fileutil.WriteFile;
import com.chenwc.util.fileutil.jarfile.Compressor;
import com.chenwc.util.fileutil.propertiesfile.PropertiesFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chenwc
 */
public class TranslatePropertiesFileMultiStep implements MultiStepJob {
    private static final Logger logger = LoggerFactory.getLogger(TranslatePropertiesFileMultiStep.class);

    //原子整数
    static AtomicInteger JOB_COUNT = new AtomicInteger(0);
    //增量计数器
    int jobID = JOB_COUNT.addAndGet(1);
    //调用数量
    int calls;
    //执行调用数量
    int executedCalls = 0;

    //异常
    Exception ex = null;

    //json参数返回，返回所有类型
    JSONObject writeDataL;

    String resourcePropertiesFile;

    LocaleLanguage localeLanguage;

    TranslateSoftware translateSoftware;

    public TranslatePropertiesFileMultiStep(int calls, String resourcePropertiesFile, LocaleLanguage localeLanguage, TranslateSoftware translateSoftware) {
        this.calls = calls;
        this.resourcePropertiesFile = resourcePropertiesFile;
        this.localeLanguage = localeLanguage;
        this.translateSoftware = translateSoftware;
    }

    /**
     * 是否完成
     *
     * @return 是否完成
     */
    @Override
    public boolean isFinished() {
        return executedCalls == calls || ex != null;
    }

    /**
     * 获取名称
     *
     * @return 名称
     */
    @Override
    public String getName() {
        return "stateless Job-" + jobID;
    }

    /**
     * 运行下一步
     */
    @Override
    public JSONObject runNextStep() {
        try {
            if (null == writeDataL) {
                writeDataL = new JSONObject();
            }
            //获取属性文件路径和文件名
            String propertiesFilePath = resourcePropertiesFile.substring(0, resourcePropertiesFile.lastIndexOf("\\"));
            logger.debug("propertiesFilePath: {}", propertiesFilePath);
            String propertiesFileName = resourcePropertiesFile.substring(resourcePropertiesFile.lastIndexOf("\\") + 1);
            logger.debug("propertiesFileName: {}", propertiesFileName);
            //新的语言配置文件路径
            String newPropertiesFileName = propertiesFileName.substring(0, propertiesFileName.lastIndexOf("."))
                    + "_" + localeLanguage.getLocale()
                    + propertiesFileName.substring(propertiesFileName.lastIndexOf("."));
            logger.debug("newPropertiesFileName: {}", newPropertiesFileName);
            String newPropertiesFilePath = propertiesFilePath + "\\" + newPropertiesFileName;
            logger.debug("newPropertiesFilePath: {}", newPropertiesFilePath);
            PropertiesFileUtil.translatePropertiesFile(resourcePropertiesFile, newPropertiesFilePath, translateSoftware);

            //执行调用数量增加
            executedCalls++;

            //如果完成
            if (isFinished()) {
                logger.info("执行完成: {}", resourcePropertiesFile);
            }
        } catch (RuntimeException je) {
            je.printStackTrace();
            ex = je;
        }
        return writeDataL;
    }

    /**
     * 清理
     */
    @Override
    public void cleanUp() {
        StringBuilder sb = new StringBuilder("任务 ").append(getName()).append(" 处理完成。 ");
        if (ex != null) {
            sb.append("出现异常：").append(ex);
        } else {
            sb.append("成功！ ");
        }
        logger.info(sb.toString());
    }
}
